﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core;
using Baosight.Platform.Core.UI;
using Baosight.Platform.Core.ServiceModel;
using Microsoft.Practices.Unity;
using Telerik.Windows.Controls;
using Baosight.ZNH.ZMCommon.ChildWindows;

namespace Baosight.ZNH.ZMCL.UI
{
    /// <summary>
    /// 选择所有在用的巡检点
    /// </summary>
    /// Title:SelectSurveyPoint
    /// Service:ServiceZMCL92
    /// Copyright:Baosight Software LTD.co Copyright (c) 2011
    /// Company:上海宝信软件股份有限公司
    /// Author: 高强
    /// Version: 1.0
    /// History:
    ///       2011-6-28  高强   [创建]
    ///  
    public partial class SelectSurveyPoint : EFViewCmes
    {
        public SelectSurveyPoint()
        {
            InitializeComponent();
            base.BeforeAction += OnBeforeAction;
        }

        /// <summary>
        /// 初始化父类EFViewCmesProperty的值，每次点击“确定”、“返回”时执行此方法，从新负值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.ViewType = "tree";
            base.ViewProperty = _view;
        }

        /// <summary>
        /// 确定按钮按下增加巡检点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void QueryClick(object sender, RoutedEventArgs e)
        {
            EIInfo eiInfo = new EIInfo();
            eiInfo.AddBlock(EIConstants.ResultBlock);
            eiInfo.Set("fdSurveyPointInfo", this.fdSurveyPointInfo.Value.ToString());
            HttpServiceAgent agent = new HttpServiceAgent();
            eiInfo.Attributes.Add(EIConstants.ServiceNameKey, "ZMCL92");
            eiInfo.Attributes.Add(EIConstants.MethodNameKey, "queryAllSurveyPoint");
            agent.CallService(eiInfo, OnDoQueryCompleted);
        }

        /// <summary>
        /// 条件查询巡检点的回调函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnDoQueryCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            if (e.EIInfo.Status == -1)
            {
                base.OpenEFWindowAlert(e.EIInfo.DetailMsg, true);
                return;
            }
            grid.UpdateData(e.EIInfo);
        }

        /// <summary>
        /// 绑定值到指定的Block内 
        /// </summary>
        /// <param name="block"></param>
        /// <param name="columnName"></param>
        /// <param name="dataValue"></param>
        private void BindToBlock(EIBlock block, string columnName)
        {
            EIColumn column = new EIColumn();
            column.SetName(columnName);
            block.AddColumn(column);
        }

        /// <summary>
        /// 确定按钮按下增加巡检点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CommitClick(object sender, RoutedEventArgs e)
        {
            Button but = sender as Button;
            but.IsEnabled = false;
            TempButton = but;
            List<EIRow> rowTemp = grid.GetCheckedEIRows();
            if (rowTemp.Count != 0)
            {
                EIBlock block = new EIBlock("myInsert");
                BindToBlock(block, "fdKeyId");
                BindToBlock(block, "fdSecuritypathId");
                BindToBlock(block, "fdSurveypointId");
                BindToBlock(block, "fdPathSerial");
                BindToBlock(block, "fdStatus");
                for (int i = 0; i < rowTemp.Count; i++)
                {
                    EIRow Row = new EIRow();
                    EIRow row = rowTemp[i];
                    Row.Add("fdSurveypointId", row["fdKeyId"].ToString());
                    block.AddRow(Row);
                }
                EIInfo eiInfo = new EIInfo();
                eiInfo.Set("service", "ZMCL92");
                eiInfo.Set("method", "insert");
                eiInfo.AddBlock(block);
                HttpServiceAgent agent = new HttpServiceAgent();
                agent.CallService(eiInfo, DoWorkCompleted, DoWorkError);
            }
            else
            {
                base.OpenEFWindowAlert("请选择需添加的巡检点！", true);
            }
        }

        /// <summary>
        /// 返回错误处理信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DoWorkError(object sender, DoWorkErroredEventArgs e)
        {
            EFWindow.Alert(e.Exception.Message);
        }

        /// <summary>
        /// 回调
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DoWorkCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            EIInfo inInfo = e.EIInfo;
            if (inInfo.Status == -1)
            {
                string _msg;
                if (inInfo.GetMsg() == null || inInfo.GetMsg() == "")
                {
                    _msg = "新增巡检点操作失败！";
                }
                else
                {
                    _msg = inInfo.GetMsg();
                }
                base.OpenEFWindowAlert(_msg, true);
            }
            else
            {
                CmesWindows _window = this.Parent as CmesWindows;
                _window.DialogResult = true;
                _window.Close();
            }
        }

        /// <summary>
        /// 取消按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CancelClick(object sender, RoutedEventArgs e)
        {
            CmesWindows _window = this.Parent as CmesWindows;
            _window.DialogResult = false;
            _window.Close();
        }
    }
}
